﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class XemDonDatHang : Form
    {
        private int _quyTrinhID;
        private string _maDonDH;

        public XemDonDatHang(int QuyTrinhID)
        {
            InitializeComponent();
            _quyTrinhID = QuyTrinhID;
        }

        public XemDonDatHang(string maDonDatHang)
        {
            InitializeComponent();
            _maDonDH = maDonDatHang;
        }

        private void XemPhieuDeNghiDenNhaCungCap_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            if (_maDonDH == null)
            {
                _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.ID == _quyTrinhID);
                cbbDonDatHang.DataSource = qt.DonDatHangs;
            }
            else
            {
                _DB.DonDatHang d = dc.DonDatHangs.Single(x => x.MaDonDatHang == _maDonDH);
                cbbDonDatHang.Items.Add(d);
                cbbDonDatHang.SelectedIndex = 0;
            }
        }

        private void cbbNhaCungCap_SelectedIndexChanged(object sender, EventArgs e)
        {
            // check
            _DB.DonDatHang d = (_DB.DonDatHang)cbbDonDatHang.SelectedItem;
            if (d == null)
                return;

            // show don dat hang info
            txtNhaCungCap.Text = d.NhaCungCap.TenNhaCungCap;

            dgvHangHoa.Rows.Clear();
            int count = 1;
            foreach (_DB.HangHoaDonDatHang h in d.HangHoaDonDatHangs)
            {
                _DB.PhieuDeNghiMuaHang_HangHoa hh = h.PhieuDeNghiMuaHang_HangHoa;
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvHangHoa);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = hh.HangHoa.TenHangHoa;
                r.Cells[2].Value = hh.SoLuongMua;
                r.Cells[3].Value = hh.HangHoa.DonVi;
                r.Cells[4].Value = hh.HangHoa_NhaCungCap.Gia;

                dgvHangHoa.Rows.Add(r);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            _DB.DonDatHang d = (_DB.DonDatHang)cbbDonDatHang.SelectedItem;
            if (d == null)
                return;
            string maphieu = d.QuyTrinhMuaHang.MaPhieuDeNghiMuaHang;
            string maNcc = d.MaNhaCungCap;
            Reports.DS_DonDatHang ds = new Reports.DS_DonDatHang();
            ds.fillData(maphieu, maNcc);
            ds.viewReport();
        }


    }
}
